#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<math.h>
#include <time.h>
#include "Car.h"

//创建一个车辆信息结构体
Car* Car_Create(char plate[PLATE_SIZE]) {
    Car* C = (Car*)malloc(sizeof(Car));
    strcpy(C->license_plate, plate);
    C->in_time = -1;
    C->out_time = -1;
    C->costs = 0.0;
    return C;
}

//计算车辆的收取费用并输出费用
double Car_Bill(Car* C) {
    double seconds = difftime(C->out_time, C->in_time);
    C->costs = seconds / 300.0; //每停留5分钟计费1元
    return (C->costs);
}

//修改车辆车牌号
Car* Car_ModifyL(Car* C, char* plate) {
    strcpy(C->license_plate, plate);
    return C;
}

//修改车辆进入时间
Car* Car_ModifyI(Car* C, time_t t) {
    C->in_time = t;
    return C;
}

//修改车辆出去时间
Car* Car_ModifyO(Car* C, time_t t) {
    C->out_time = t;
    return C;
}

//修改车辆计费
Car* Car_ModifyC(Car* C, double cost) {
    C->costs = cost;
    return C;
}

//输出车辆的信息，存在返回1，没有返回0
int Car_Check(Car* C) {
    if (C == NULL) {
        return 0;
    }
    else {
        struct tm* inT = NULL, * outT = NULL, * nowT = NULL;
        if (C->in_time != -1)
            inT = localtime(&C->in_time);
        if (C->out_time != -1)
            outT = localtime(&C->out_time);
        time_t now_time;
        time(&now_time);//这里获取了当前时间后不要将now_time用localtime()函数转化成tm结构体，不然进入时间会随着当前时间而变化

        if (inT != NULL && outT == NULL) {
            printf("车牌号：%s\n", C->license_plate);
            char in_time_str[20];
            strftime(in_time_str, sizeof(in_time_str), "%Y-%m-%d %H:%M:%S", inT);
            printf("进入时间：%s\n", in_time_str);

            printf("出去时间：暂无\n");

            printf("停留时间：");
            int s = int(difftime(now_time, C->in_time));
            int min = s / 60;
            int h = min / 60;
            int day = h / 24;
            s = s % 60;
            min = min % 60;
            h = h % 24;
            printf("%d天%d小时%d分钟%d秒\n", day, h, min, s);
            printf("计费：");
            double seconds = difftime(now_time, C->in_time);
            C->costs = seconds / 300.0;
            printf("%.2f元\n", C->costs);
        }
        else if (inT != NULL && outT != NULL) {
            printf("车牌号：%s\n", C->license_plate);

            char in_time_str[20];
            strftime(in_time_str, sizeof(in_time_str), "%Y-%m-%d %H:%M:%S", inT);
            printf("进入时间：%s\n", in_time_str);

            char out_time_str[20];
            strftime(out_time_str, sizeof(out_time_str), "%Y-%m-%d %H:%M:%S", outT);
            printf("离开时间：%s\n", out_time_str);

            printf("停留时间：");
            int s = int(difftime(C->out_time, C->in_time));
            int min = s / 60;
            int h = min / 60;
            int day = h / 24;
            s = s % 60;
            min = min % 60;
            h = h % 24;
            printf("%d天%d小时%d分钟%d秒\n", day, h, min, s);
            printf("计费：");
            double seconds = difftime(now_time, C->in_time);
            C->costs = seconds / 300.0;
            printf("%.2f元\n", C->costs);
        }
        return 1;
    }

}